<?php
	
	header('Content-type:text/html;charset=utf-8');
	//后台商品信息控制器
	class GoodsAction extends Action{
		
		//显示添加商品页面
		public function addgoods(){
			//实例化商品分类模型类
			$cate=D('Category');
			//查找分级ID为0的一级分类的数据
			$data=$cate->select();
			//分配数据到模版文件
			$this->assign('data',$data);
			
			//实例化品牌模型
			$brand=D('Brand');
			$data1=$brand->select();
			$this->assign('data1',$data1);
			$this->display('addgoods');
		}
		
		//添加商品信息方法
		public function insert(){
			if(isset($_POST['submit'])){
				//加载自动添加货号
				import('@.Common.Snauto');
				//调用自动货号方法
				$Snauto=new Snauto();
				//判断用户是否输入了货号
				$sn=$Snauto->sn();
				if($_POST['g_sn']==''){
					$_POST['g_sn']=$sn;
				}				
				//导入文件上传类
				import('ORG.Net.UploadFile');
				import( 'ORG.Util.Image');
				//实例化
				$upload=new UploadFile();
				// 使用对上传图片进行缩略图处理
				$upload->thumb   =  true;
				// 图库类包路径
				$upload-> imageClassPath = 'ORG.Util.Image';
				// 缩略图最大宽度
				$upload-> thumbMaxWidth='110';
				// 缩略图最大高度
				$upload-> thumbMaxHeight='90';
				// 缩略图前缀
				$upload-> thumbPrefix   =  'thumb_';
				$time=time();
				$date=date('Ymd',$time);
				
				// 缩略图保存路径
				$upload-> thumbPath = './Public/Uploads/'.$date.'/';
				// 缩略图文件名
				$upload-> thumbFile		='time';
				// 是否移除原图
				$upload-> thumbRemoveOrigin = false;
				//设置保存命名规则
				$upload->saveRule='time';
				//开启子目录保存文件
				$upload->autoSub=true;
				//使用date函数生成目录结构
				$upload->subType='date';
				//设置子目录的命名
				$upload->dateFormat='Ymd';
				//设置文件保存路径
				$upload->savePath='./Public/Uploads/';
				//判断上传是否成功
				if($upload->upload()){
					//成功，获取相关信息
					$info=$upload->getUploadFileInfo();					
				}else{
					//失败
					$this->error($upload->getErrorMsg(),'addgoods');
					exit;
				}
				//将上传文件的字段自动添加到POST请求，然后保存上传成功的文件名称
				$_POST['g_img']=$info[0]['savename'];
				//把保存的文件名字分割为数组
				$arr=explode('/', $info[0]['savename']);
				//拼凑缩略图的前缀
				$str='thumb_'.$arr[1];
				$str1=$arr[0].'/'.$str;
				//存放缩略图的保存地址到数据库		
				$_POST['g_thumb_img']=$str1;
				//实例化模型
				$goods=D('Goods');
				$goods->create();
				if($goods->add()){
					$this->success('添加成功','addgoods');
				}else{
					$this->error('添加失败','addgoods');
				}
			}
		}
		
		//显示商品信息管理页面
		public function goods(){
			import('ORG.Util.Page');
			//实例化模型类
			$goods=D('Goods');
			//查找分类的数据
			$data=$goods->select();
			//计算出数据的总数
			$count=$goods->count();
			//实例化分页对象,把总数和分页要显示的行数传递过去
			$page=new Page($count,8);
			//调用show方法
			$show=$page->show();
			//分配到模版
			$this->assign('show',$show);
			//多表查询table方法匹配到的
			/* $data=$goods->table('hfp_goods t1,hfp_category t2,hfp_brand t3')
			->where('t1.c_id=t2.c_id and t1.b_id=t3.b_id')
			->limit($page->firstRow . ',' . $page->listRows)->select(); */
			
			/**
			 * 以下是用jion多表查询方法（全部数据都查询出来，包括没有匹配到的）
			 */			
			 $data=$goods->join('hfp_category on hfp_goods.c_id=hfp_category.c_id')
			->join('hfp_brand on hfp_goods.b_id=hfp_brand.b_id')
			->limit($page->firstRow . ',' . $page->listRows)->order('g_id desc')->select();
			
			 //实例化商品分类模型类
			 $cate=D('Category');
			 	
			 $data1=$cate->select();
			 //分配数据到模版文件
			 $this->assign('data1',$data1);
			 
			 //实例化品牌模型
			 $brand=D('Brand');
			 $data2=$brand->select();
			 $this->assign('data2',$data2);
			 
			$this->assign('data',$data);
			$this->display('goods');
		}
		
		//显示商品修改页面
		public function edit(){
			$g_id=isset($_GET['id']) ? $_GET['id'] :'';
		
			
			$goods=D('Goods');
			$row=$goods->join('hfp_category on hfp_goods.c_id=hfp_category.c_id')
			->join('hfp_brand on hfp_goods.b_id=hfp_brand.b_id')
			->limit($page->firstRow . ',' . $page->listRows)->find($g_id);
			
			//分配数据到模版文件
			$this->assign('row',$row);
			$this->display('edit');
		}
		
		function sn(){
			import('@.Common.Snauto');
			
			//调用静态方法
			$Snauto=new Snauto();
			$sn=$Snauto->sn();
			var_dump($sn);
			exit;
		}
		
		//商品修改方法
		public function update(){
			if(isset($_POST['submit'])){
				//加载自动添加货号
				import('@.Common.Snauto');
				//调用自动货号方法
				$Snauto=new Snauto();
				//判断用户是否输入了货号
				$sn=$Snauto->sn();				
				if($_POST['g_sn']==''){
					$_POST['g_sn']=$sn;
				}
				
				//实例化模型
				$goods=D('Goods');
					
				$g_id=$_POST['g_id'];
				$row = $goods->find ( $g_id);
			
				//导入文件上传类
				import('ORG.Net.UploadFile');
				import( 'ORG.Util.Image');
				//实例化
				$upload=new UploadFile();
				// 使用对上传图片进行缩略图处理
				$upload->thumb   =  true;
				// 图库类包路径
				$upload-> imageClassPath = 'ORG.Util.Image';
				// 缩略图最大宽度
				$upload-> thumbMaxWidth='110';
				// 缩略图最大高度
				$upload-> thumbMaxHeight='90';
				// 缩略图前缀
				$upload-> thumbPrefix   =  'thumb_';
				$time=time();
				$date=date('Ymd',$time);
				
				// 缩略图保存路径
				$upload-> thumbPath = './Public/Uploads/'.$date.'/';
				// 缩略图文件名
				$upload-> thumbFile		='time';
				// 是否移除原图
				$upload-> thumbRemoveOrigin = false;
				//设置保存命名规则
				$upload->saveRule='time';
				//开启子目录保存文件
				$upload->autoSub=true;
				//使用date函数生成目录结构
				$upload->subType='date';
				//设置子目录的命名
				$upload->dateFormat='Ymd';
				//设置文件保存路径
				$upload->savePath='./Public/Uploads/';
				//判断上传是否成功
				if($upload->upload()){
					//成功，获取相关信息
					$info=$upload->getUploadFileInfo();
					
					
				}else{
					//失败
					$this->error($upload->getErrorMsg(),'addgoods');
					exit;
				}
				//将上传文件的字段自动添加到POST请求，然后保存上传成功的文件名称
				$_POST['g_img']=$info[0]['savename'];
				//把保存的文件名字分割为数组
				$arr=explode('/', $info[0]['savename']);
				//拼凑缩略图的前缀
				$str='thumb_'.$arr[1];
				$str1=$arr[0].'/'.$str;
				//存放缩略图的保存地址到数据库		
				$_POST['g_thumb_img']=$str1;
				
				//实例化模型
				$goods=D('Goods');
			
				$goods->create();
				
				if($goods->save()){
					if ($row ['g_img'] || $row ['g_thumb_img']) {
						// 判断删除图片是否成功
						unlink ( './Public/Uploads/' . $row ['g_img'] );
						unlink ( './Public/Uploads/' . $row ['g_thumb_img'] );
					}
					$this->success('修改商品信息成功','goods');
				}else{
					$this->error('修改商品信息失败','edit');
				}
				}
			}
		
		//删除单个商品数据
		public function del(){
			$g_id=isset($_GET['id']) ? $_GET['id'] :'';
			$goods=D('Goods');
			$row = $goods->find ( $g_id);
			if ($row ['g_img'] || $row ['g_thumb_img']) {
				// 判断删除图片是否成功
				if (! (unlink ( './Public/Uploads/' . $row ['g_img'] ) && unlink ( './Public/Uploads/' . $row ['g_thumb_img'] ))) {
					echo '删除失败';
					exit ();
				}
			}
			$del=$goods->delete($g_id);
				if($del){
					$this->success('删除成功','__URL__/goods');
				}else{
					$this->error('删除失败','__URL__/goods');
				}	
		}
		
		//删除多个商品方法
		public function deleteAll(){
		if (isset ( $_POST ['deleteSubmit'] )) {
		
			$goods = D ( 'Goods' );
			$id = $_POST ['id'];
			for($i = 0; $i < count ( $id ); $i ++) {
				$row = $goods->find ( $id [$i] );
				
				// 判断图片字段是否有值
				if ($row ['g_img'] || $row ['g_thumb_img']) {
					// 判断删除图片是否成功
					if (! (unlink ( './Public/Uploads/' . $row ['g_img'] ) || unlink ( './Public/Uploads/' . $row ['g_thumb_img'] ))) {
						echo '删除失败:./Public/Uploads/' . $row ['g_img'].'或者'.$row ['g_thumb_img'];
							exit ();
							}
						}
						$goods->delete ( $id [$i] );
						}
					$this->success ( '删除成功', 'goods' );
					}
				}
		
				public function checkNohot(){
					$g_id=$_POST['g_id'];										
					$g_hot=$_POST['g_hot'];
					//实例化模型类					
					$goods=D('Goods');
					//查找分级ID为0的一级分类的数据
					$data=$goods->where("g_id='$g_id'")->save(array(
							'g_is_hot'=>$g_hot							
					));
					$this->ajaxReturn($data,'查询完毕',1);
				}
				
				public function checkYeshot(){
					$g_id=$_POST['g_id'];												
					$g_hot=$_POST['g_hot'];
					//实例化模型类
					$goods=D('Goods');
					//查找分级ID为0的一级分类的数据
					$data=$goods->where("g_id='$g_id'")->save(array(
							'g_is_hot'=>$g_hot
								
					));
					$this->ajaxReturn($data,'查询完毕',1);
				}
				
				public function checkNonew(){
					$g_id=$_POST['g_id'];								
					$g_new=$_POST['g_new'];
					//实例化模型类				
					$goods=D('Goods');				
					//查找分级ID为0的一级分类的数据
					$data=$goods->where("g_id='$g_id'")->save(array(
							'g_is_new'=>$g_new
				
					));
					$this->ajaxReturn($data,'查询完毕',1);
				}
				
				public function checkYesnew(){
					$g_id=$_POST['g_id'];								
					$g_new=$_POST['g_new'];
					//实例化模型类				
					$goods=D('Goods');				
					//查找分级ID为0的一级分类的数据
					$data=$goods->where("g_id='$g_id'")->save(array(
							'g_is_new'=>$g_new				
					));
					$this->ajaxReturn($data,'查询完毕',1);
				}
				
				public function checkNosale(){
					$g_id=$_POST['g_id'];
					$g_sale=$_POST['g_sale'];
					//实例化模型类
					$goods=D('Goods');
					//查找分级ID为0的一级分类的数据
					$data=$goods->where("g_id='$g_id'")->save(array(
							'g_is_sale'=>$g_sale
					));
					$this->ajaxReturn($data,'查询完毕',1);
				}
				
				/* 页面点击上架更新数据  */
				public function checkYessale(){
					$g_id=$_POST['g_id'];
					$g_sale=$_POST['g_sale'];
					//实例化模型类
					$goods=D('Goods');
					//查找分级ID为0的一级分类的数据
					$data=$goods->where("g_id='$g_id'")->save(array(
							'g_is_sale'=>$g_sale
					));
					$this->ajaxReturn($data,'查询完毕',1);
				}
				
				/*
				 * 实现商品信息页搜索
				 *
				*/
				public function search(){
					$c_id=$_GET['c_id'];
					$b_id=$_GET['b_id'];
					$g_name=$_GET['g_name'];
					import('ORG.Util.Page');
					//实例化模型类
					$goods=D('Goods');
					
					if(empty($_GET['g_name']) && empty($_GET['b_id']) && empty($_GET['c_id'])){
						$count=$goods->count();
						//实例化分页对象,把总数和分页要显示的行数传递过去
						$page=new Page($count,8);
						$data=$goods->join('hfp_category on hfp_goods.c_id=hfp_category.c_id')
						->join('hfp_brand on hfp_goods.b_id=hfp_brand.b_id')
						->limit($page->firstRow . ',' . $page->listRows)->order('g_id desc')
						->select();
						
							
					}elseif(empty($_GET['c_id']) && empty($_GET['b_id'])){
						$count=$goods->where("g_name like '%$g_name%'")->count();
						//实例化分页对象,把总数和分页要显示的行数传递过去
						$page=new Page($count,1);
						$data=$goods->join('hfp_category on hfp_goods.c_id=hfp_category.c_id')
						->join('hfp_brand on hfp_goods.b_id=hfp_brand.b_id')
						->limit($page->firstRow . ',' . $page->listRows)->order('g_id desc')
						->where("g_name like '%$g_name%'")
						
						->select();
					}elseif(empty($_GET['c_id']) && empty($_GET['g_name'])){
						$count=$goods->where("hfp_goods.b_id='$b_id'")->count();
						//实例化分页对象,把总数和分页要显示的行数传递过去
						$page=new Page($count,1);
						$data=$goods->join('hfp_category on hfp_goods.c_id=hfp_category.c_id')
						->join('hfp_brand on hfp_goods.b_id=hfp_brand.b_id')
						->where("hfp_goods.b_id='$b_id'")
						->limit($page->firstRow . ',' . $page->listRows)->order('g_id desc')
						->select();
					}elseif(empty($_GET['b_id']) && empty($_GET['g_name'])){
						$count=$goods->where("hfp_goods.c_id='$c_id'")->count();
						//实例化分页对象,把总数和分页要显示的行数传递过去
						$page=new Page($count,1);
						$data=$goods->join('hfp_category on hfp_goods.c_id=hfp_category.c_id')
						->join('hfp_brand on hfp_goods.b_id=hfp_brand.b_id')
						->where("hfp_goods.c_id='$c_id'")
						->limit($page->firstRow . ',' . $page->listRows)->order('g_id desc')
						->select();
					}elseif(empty($_GET['b_id'])){
						$count=$goods->where("hfp_goods.c_id='$c_id' and g_name like '%$g_name%'")->count();
						//实例化分页对象,把总数和分页要显示的行数传递过去
						$page=new Page($count,1);
						$data=$goods->join('hfp_category on hfp_goods.c_id=hfp_category.c_id')
						->join('hfp_brand on hfp_goods.b_id=hfp_brand.b_id')
						->where("hfp_goods.c_id='$c_id' and g_name like '%$g_name%'")
						->limit($page->firstRow . ',' . $page->listRows)->order('g_id desc')
						->select();
					}elseif(empty($_GET['g_name'])){
						$count=$goods->where("hfp_goods.c_id='$c_id' and hfp_goods.b_id='$b_id'")->count();
						//实例化分页对象,把总数和分页要显示的行数传递过去
						$page=new Page($count,1);
						$data=$goods->join('hfp_category on hfp_goods.c_id=hfp_category.c_id')
						->join('hfp_brand on hfp_goods.b_id=hfp_brand.b_id')
						->where("hfp_goods.c_id='$c_id' and hfp_goods.b_id='$b_id'")
						->limit($page->firstRow . ',' . $page->listRows)->order('g_id desc')
						->select();
					}elseif(empty($_GET['c_id'])){
						$count=$goods->where("hfp_goods.b_id='$b_id' and g_name like '%$g_name%'")->count();
						//实例化分页对象,把总数和分页要显示的行数传递过去
						$page=new Page($count,1);
						$data=$goods->join('hfp_category on hfp_goods.c_id=hfp_category.c_id')
					->join('hfp_brand on hfp_goods.b_id=hfp_brand.b_id')
					->where("hfp_goods.b_id='$b_id' and g_name like '%$g_name%'")
					->limit($page->firstRow . ',' . $page->listRows)->order('g_id desc')
					->select(); 
					}
					
					//调用show方法
					$show=$page->show();
					//分配到模版
					$this->assign('show',$show);
					 //实例化商品分类模型类
					 $cate=D('Category');
					 	
					 $data1=$cate->select();
					 //分配数据到模版文件
					 $this->assign('data1',$data1);
					 
					 //实例化品牌模型
					 $brand=D('Brand');
					 $data2=$brand->select();
					 $this->assign('data2',$data2);
					 
					$this->assign('data',$data);
					$this->display('goods');
					
					
				}
	}